@echo off
setlocal enabledelayedexpansion

REM Weather System 日志管理工具 (Windows)

set SCRIPT_DIR=%~dp0
set PROJECT_DIR=%SCRIPT_DIR%..

if "%1"=="" goto :help
if "%1"=="help" goto :help
if "%1"=="-h" goto :help
if "%1"=="--help" goto :help

goto :%1

:help
echo Weather System 日志管理工具
echo.
echo 用法: %0 [COMMAND] [OPTIONS]
echo.
echo 命令:
echo   view [service]     - 查看服务日志
echo   follow [service]   - 实时跟踪日志
echo   search ^<term^>      - 搜索日志内容
echo   errors             - 查看错误日志
echo   export [date]      - 导出指定日期的日志
echo   stats              - 显示日志统计信息
echo   cleanup [days]     - 清理指定天数前的日志
echo   grafana            - 打开Grafana日志面板
echo.
echo 服务名称:
echo   api, frontend, rabbitmq, redis, loki, promtail, grafana
echo.
echo 示例:
echo   %0 view api
echo   %0 follow api
echo   %0 search "数据采集失败"
echo   %0 errors
goto :eof

:view
set service=%2
set lines=%3
if "%lines%"=="" set lines=100

if "%service%"=="" set service=all

if "%service%"=="all" (
    echo 查看所有服务日志 ^(最近 %lines% 行^)
    docker-compose logs --tail=%lines%
) else (
    call :get_container_name %service%
    if "!container_name!"=="" (
        echo 错误: 未知的服务名称: %service%
        goto :eof
    )
    echo 查看 %service% 服务日志 ^(最近 %lines% 行^)
    docker logs --tail=%lines% !container_name!
)
goto :eof

:follow
set service=%2
if "%service%"=="" set service=all

if "%service%"=="all" (
    echo 实时跟踪所有服务日志 ^(Ctrl+C 退出^)
    docker-compose logs -f
) else (
    call :get_container_name %service%
    if "!container_name!"=="" (
        echo 错误: 未知的服务名称: %service%
        goto :eof
    )
    echo 实时跟踪 %service% 服务日志 ^(Ctrl+C 退出^)
    docker logs -f !container_name!
)
goto :eof

:search
set search_term=%2
if "%search_term%"=="" (
    echo 错误: 请提供搜索关键词
    goto :eof
)

echo 搜索日志: '%search_term%'
docker-compose logs | findstr /i "%search_term%"
goto :eof

:errors
set since=%2
if "%since%"=="" set since=1h

echo 查看错误日志 ^(最近 %since%^)
docker-compose logs --since=%since% | findstr /i "error exception fail critical"
goto :eof

:export
set export_date=%2
if "%export_date%"=="" (
    for /f "tokens=1-3 delims=/" %%a in ('date /t') do set export_date=%%c-%%a-%%b
)

set export_dir=%PROJECT_DIR%logs\exports
if not exist "%export_dir%" mkdir "%export_dir%"

echo 导出 %export_date% 的日志到 %export_dir%
docker-compose logs --since=%export_date% > "%export_dir%\weather-system-%export_date%.log"

echo 日志导出完成: %export_dir%
dir "%export_dir%"
goto :eof

:stats
echo Weather System 日志统计
echo.

echo API日志统计:
curl -s "http://localhost:5197/api/logs/stats" 2>nul || echo API未运行或统计不可用
echo.

echo 容器状态:
docker-compose ps
echo.

if exist "%PROJECT_DIR%logs" (
    echo 本地日志文件:
    dir "%PROJECT_DIR%logs\" /o-d
)
goto :eof

:cleanup
set days=%2
if "%days%"=="" set days=30

echo 清理 %days% 天前的日志文件

if exist "%PROJECT_DIR%logs" (
    forfiles /p "%PROJECT_DIR%logs" /m *.log /d -%days% /c "cmd /c del @path" 2>nul
    echo 本地日志清理完成
)

docker system prune -f --filter "until=%days%h"
echo Docker日志清理完成
goto :eof

:grafana
set grafana_url=http://localhost:3001

echo 打开Grafana日志面板
echo URL: %grafana_url%
echo 用户名: admin
echo 密码: admin123

start "" "%grafana_url%"
goto :eof

:get_container_name
set service=%1
set container_name=

REM 检查是否为开发环境
if exist "%PROJECT_DIR%docker-compose.dev.yml" (
    docker-compose -f "%PROJECT_DIR%docker-compose.dev.yml" ps | findstr weather >nul
    if !errorlevel!==0 set is_dev=true
)

if "%service%"=="api" (
    if "%is_dev%"=="true" (
        set container_name=weather-system-api
    ) else (
        set container_name=weather-system-api
    )
) else if "%service%"=="frontend" (
    set container_name=weather-system-frontend
) else if "%service%"=="rabbitmq" (
    if "%is_dev%"=="true" (
        set container_name=weather-rabbitmq-dev
    ) else (
        set container_name=weather-rabbitmq
    )
) else if "%service%"=="redis" (
    if "%is_dev%"=="true" (
        set container_name=weather-redis-dev
    ) else (
        set container_name=weather-redis
    )
) else if "%service%"=="loki" (
    if "%is_dev%"=="true" (
        set container_name=weather-loki-dev
    ) else (
        set container_name=weather-loki
    )
) else if "%service%"=="grafana" (
    if "%is_dev%"=="true" (
        set container_name=weather-grafana-dev
    ) else (
        set container_name=weather-grafana
    )
)
goto :eof